/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tinckay.common.log.factory;

import com.tinckay.common.constant.LogType;
import com.tinckay.common.log.LogManager;
import com.tinckay.common.util.SpringContextHolder;
import com.tinckay.entity.po.SysLog;
import com.tinckay.entity.po.User;
import com.tinckay.mapper.SysLogMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TimerTask;


/**
 * 日志操作任务创建工厂
 *
 * @author yuanjie
 */
public class LogTaskFactory {

    private static Logger logger = LoggerFactory.getLogger(LogManager.class);
    private static SysLogMapper sysLogMapper = SpringContextHolder.getBean(SysLogMapper.class);

    public static TimerTask sysLog(final LogType logType, final User user, String operation, Long time, String methodName, String params, String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    SysLog loginLog = LogFactory.createSysLog(logType, user, operation, time, methodName, params, ip);
                    sysLogMapper.insert(loginLog);
                } catch (Exception e) {
                    logger.error("创建{}日志异常,cause:{}", logType.getMessage(), e);
                }
            }
        };
    }

    //public static TimerTask exitLog(final Integer userId, final String ip) {
    //    return new TimerTask() {
    //        @Override
    //        public void run() {
    //            SysLog loginLog = LogFactory.createSysLog(LogType.EXIT, userId, null, ip);
    //            try {
    //                sysLogMapper.insert(loginLog);
    //            } catch (Exception e) {
    //                logger.error("创建退出日志异常!", e);
    //            }
    //        }
    //    };
    //}

    //public static TimerTask exceptionLog(final Integer userId, final Exception exception) {
    //    return new TimerTask() {
    //        @Override
    //        public void run() {
    //            String msg = ToolUtils.getExceptionMsg(exception);
    //            Syslog sysLog = LogFactory.createSyslog(
    //                    LogType.EXCEPTION, userId, "", null, null, msg, LogSucceed.FAIL);
    //            try {
    //                sysLogMapper.insert(sysLog);
    //            } catch (Exception e) {
    //                logger.error("创建异常日志异常!", e);
    //            }
    //        }
    //    };
    //}
}
